package com.example.gramediamajalahmobile.channel;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;

import com.example.gramediamajalahmobile.R;
import com.example.gramediamajalahmobile.favorite.FavoriteList;
import com.example.gramediamajalahmobile.main.Article;
import com.example.gramediamajalahmobile.sqlite.DBHelperChannel;

public class Channel extends Activity implements OnItemClickListener {
	public static final String AR_CHANNEL = "channel";
	// private static final int INSERT_ID = Menu.FIRST;
	EditText empty;
	private ListView listview;
	private DBHelperChannel db;
	Button fav , add ;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.channel);
		db = new DBHelperChannel(this);
		db.open();

		listview = (ListView) findViewById(R.id.list);
	//	listview.setEmptyView(findViewById(R.id.empty));
		listview.setOnItemClickListener(this);
		fav = (Button) findViewById(R.id.imageButton1);
		add = (Button) findViewById(R.id.imageButton2);

		//memanggil method
		loadData();
		fav.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(Channel.this, FavoriteList.class);
				startActivity(in);
				db.close();
			}
		});
		add.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent in = new Intent(Channel.this, KategoriChannel.class);
				startActivity(in);
				db.close();
			}
		});
	}

	private void loadData() {
		// TODO Auto-generated method stub
		//mengambil data semua article
		Cursor cur = db.getAllChannel();
		//mengambil data title
		String[] from = new String[] { DBHelperChannel.COL_NAME,DBHelperChannel.COL_IMAGES};
		//menaruh pada ID title
		int[] to = new int[] { R.id.textchannel,R.id.gambaar};
		//tampil pada listview
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.rowchannel, cur, from, to);
		listview.setAdapter(adapter);
	}

	// @Override
	// public boolean onCreateOptionsMenu(Menu menu)
	// {
	// // Inflate the menu; this adds items to the action bar if it is present.
	// menu.add(0, INSERT_ID, 0, R.string.menu_add);
	// return true;
	// }

	// @Override
	// public boolean onOptionsItemSelected(MenuItem item)
	// {
	// // TODO Auto-generated method stub
	// switch (item.getItemId())
	// {
	// case INSERT_ID:
	// startActivity(new Intent(FavoriteList.this, Favorite.class));
	// break;
	//
	// }
	// return super.onOptionsItemSelected(item);
	// }

	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int pos, long id) {
		// TODO Auto-generated method stub
		// Bundle bundle = new Bundle();
		// bundle.putLong(KEY_ID, id);
		Intent intent = new Intent(Channel.this, Article.class);
		intent.putExtra(AR_CHANNEL, id);
		db.close();
		startActivity(intent);

	}

}
